package net.araim.bbingo.adapters;

import net.araim.bbingo.R;
import net.araim.bbingo.ToggleImageButton;
import net.araim.bbingo.activity.EditTextsActivity;
import net.araim.bbingo.activity.EditTextsActivity.TextsDeleter;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class EditTextsViewBinder implements SimpleCursorAdapter.ViewBinder {

	private EditTextsActivity.TextsDeleter activityCallback = null;

	private OnClickListener deleteOnClick = new OnClickListener() {

		public void onClick(View v) {
			ToggleImageButton tib = ((ToggleImageButton) v.findViewById(R.id.DeleteSelectedToggle));
			if (tib.toggle()) {
				if (EditTextsViewBinder.this.activityCallback != null) {
					EditTextsViewBinder.this.activityCallback.registerDeletion(v);
				}
			} else {
				if (EditTextsViewBinder.this.activityCallback != null) {
					EditTextsViewBinder.this.activityCallback.unRegisterDeletion(v);
				}
			}
		}
	};

	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		if (view.getId() == R.id.TextListItemId) {
			((TextView) view).setText(cursor.getString(1));
			((View) view.getParent()).setOnClickListener(deleteOnClick);
			view.setTag(cursor.getInt(0));
			return true;
		} else if (view.getId() == R.id.DeleteSelectedToggle) {
			((ToggleImageButton) view).setOn(EditTextsViewBinder.this.activityCallback.isRegisteredToDelete(cursor.getInt(0)));
			return true;
		}
		return false;
	}

	public EditTextsViewBinder(TextsDeleter textsDeleter) {
		activityCallback = textsDeleter;
	}

}
